﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SERIX.Classes;
using System.Data;

namespace SERIX
{
    public partial class Planejamento : Seguranca
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void odsPlanejamentos_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = UsuAut.Login;
        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            int ano_plo = Convert.ToInt32(tbxAno.Text);
            CPlanejamento.Insert(ano_plo, UsuAut.Login);
            gridView.DataBind();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbx = (CheckBox)e.Row.FindControl("cbxPlanejamento");
                if(((DataRowView)e.Row.DataItem)["planejamento_plo"].ToString() == "1")
                    cbx.Checked = true;
                cbx = (CheckBox)e.Row.FindControl("cbxProgramacao");
                if (((DataRowView)e.Row.DataItem)["programacao_plo"].ToString() == "1")
                    cbx.Checked = true;
                cbx = (CheckBox)e.Row.FindControl("cbxExecucao");
                if (((DataRowView)e.Row.DataItem)["execucao_plo"].ToString() == "1")
                    cbx.Checked = true;
            }
        }

        private void AtualizarGrid(bool atualizar)
        {
            if (atualizar)
                gridView.DataBind();
        }

        protected void cbxProgramacao_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            int index = ((GridViewRow)cbx.Parent.Parent).RowIndex;
            int idt_Plo = Convert.ToInt32(gridView.DataKeys[index]["IDT_PLO"]);
            CPlanejamento.ManipularProgramacao(idt_Plo, cbx.Checked, UsuAut.Login);
            AtualizarGrid(cbx.Checked);

        }

        protected void cbxPlanejamento_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            int index = ((GridViewRow)cbx.Parent.Parent).RowIndex;
            int idt_Plo = Convert.ToInt32(gridView.DataKeys[index]["IDT_PLO"]);
            CPlanejamento.ManipularPlanejamento(idt_Plo, cbx.Checked, UsuAut.Login);
            AtualizarGrid(cbx.Checked);
        }

        protected void cbxExecucao_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            int index = ((GridViewRow)cbx.Parent.Parent).RowIndex;
            int idt_Plo = Convert.ToInt32(gridView.DataKeys[index]["IDT_PLO"]);
            CPlanejamento.ManipularExecucao(idt_Plo, cbx.Checked, UsuAut.Login);
            AtualizarGrid(cbx.Checked);
        }

        protected void tbxQtd_TextChanged(object sender, EventArgs e)
        {
            TextBox tbx = (TextBox)sender;
            int index = ((GridViewRow)tbx.Parent.Parent).RowIndex;
            int idt_Plo = Convert.ToInt32(gridView.DataKeys[index]["IDT_PLO"]);
            int qtd = 0;
            if (!string.IsNullOrEmpty(tbx.Text))
                qtd = Convert.ToInt32(tbx.Text);
            CPlanejamento.DefinirQtdPlanejada(idt_Plo, qtd, UsuAut.Login);
        }
    }
}
